package vn.cuhanh.sys;

import org.json.JSONException;
import org.json.JSONObject;

import vn.cuhanh.R;
import vn.cuhanh.ShareActivity;
import vn.cuhanh.UpdateActivity;
import vn.cuhanh.utils.HelperFunc;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;

public class UpdateHelper {
	public static final String PACKAGE_NAME = "vn.cuhanh";
	
	public static void checkForUpdate(final Context ctx) {
		Thread thread = new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
			
				String res = HelperFunc.getDataFromSv(ctx.getResources().getString(R.string.getMaxVersionCodeUrl));
				if(res == null) {
					//No network, do nothing
		//			((Activity)ctx).runOnUiThread(new Runnable() {
		//				
		//				@Override
		//				public void run() {
		//					// TODO Auto-generated method stub
		//					HelperFunc.showSimpleAlertDialog(ctx, 
		//							ctx.getResources().getString(R.string.fail), 
		//							ctx.getResources().getString(R.string.no_network_retry), 
		//							ctx.getResources().getString(R.string.bt_ok_title), 
		//							new DialogInterface.OnClickListener() {
		//								
		//								@Override
		//								public void onClick(DialogInterface arg0, int arg1) {
		//									// TODO Auto-generated method stub
		//									checkForUpdate(ctx);
		//								}
		//							}, 
		//							ctx.getResources().getString(R.string.bt_cancel_title), 
		//							null, true);
		//				}
		//			});
//					HelperFunc.showAlertDialogOnUiThread(ctx, ctx.getResources().getString(R.string.fail), 
//							ctx.getResources().getString(R.string.no_network_2));
					return;
				}
				
				try {
					JSONObject jsonRes = new JSONObject(res);
					if(jsonRes.getInt("result") != 1) {
						return;
					}
					ShareActivity.downloadRef = jsonRes.getString("downloadRef");
					int maxVersionCode = jsonRes.getInt("maxVersionCode");
					int curVersion = ctx.getPackageManager().getPackageInfo(
							PACKAGE_NAME, 0).versionCode;
					if(maxVersionCode > curVersion) {
						final String downloadLink = jsonRes.getString("downloadLink"); 
						((Activity)ctx).runOnUiThread(new Runnable() {
							
							@Override
							public void run() {
								// TODO Auto-generated method stub
								askForUpdate(ctx, downloadLink);
							}
						});
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NameNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}	
			}
		});
		thread.start();
	}
	
	public static void askForUpdate(final Context ctx, final String downloadLink) {
		HelperFunc.showSimpleAlertDialog(ctx, 
				ctx.getResources().getString(R.string.new_update_available_title), 
				ctx.getResources().getString(R.string.new_update_available_message), 
				ctx.getResources().getString(R.string.bt_ok_title), 
				new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						HelperFunc.trackEvent(ctx, "check_update", "btn_ok_press", "btn_ok_press");
						Intent intent = new Intent(ctx, UpdateActivity.class);
						intent.putExtra("vn.cuhanh.UpdateActivity.url", downloadLink);
						ctx.startActivity(intent);
					}
				}, 
				ctx.getResources().getString(R.string.bt_cancel_title), 
				null, true);
	}
}